package com.xnfh.mine.ui.fragment

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.xnfh.common.constant.ArouterPath
import com.xnfh.common.manager.UserInfoManager
import com.xnfh.common.provider.LoginServiceProvider
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.bean.User
import com.xnfh.framework.utils.ActManager
import com.xnfh.image.ImageHelper
import com.xnfh.mine.R
import com.xnfh.mine.databinding.FragmentMineLayoutBinding
import com.xnfh.mine.ui.activity.FeedbackActivity
import com.xnfh.mine.ui.activity.UserInforActivity
import com.xnfh.network.manager.TokenManager


/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/8 14:39
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/8 14:39
 * @UpdateRemark: 更新说明
 */
@Route(path = ArouterPath.MINE.MINE_FRAGMENT)
class MineFragment : BaseFragment<FragmentMineLayoutBinding>(), View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.fragment_mine_layout
    override fun initView(view: View, savedInstanceState: Bundle?) {
        mBind.mineUserMessage.setOnClickListener(this)
        mBind.mineFeedback.setOnClickListener(this)
        mBind.mineAboutApp.setOnClickListener(this)
        mBind.mineLogout.setOnClickListener(this)
        val userInfo = UserInfoManager.instance.getUserInfo()
        //用户名称
        mBind.mineUserName.text = userInfo?.suAccount
        //用户手机号
        mBind.mineUserPhone.text = UserInfoManager.instance.getUserPhone()
        //用户头像
        val userHeader = UserInfoManager.instance.getUserHeader()
        ImageHelper.instance?.loadCircleImage(
            requireContext(),
            userHeader,
            mBind.mineHeaderImg,
            R.mipmap.default_avatar_img,
            R.mipmap.default_avatar_img
        )
    }

    override fun observe() {

    }

    override fun onClick(v: View?) {
        when (v) {
            mBind.mineLogout -> {
                XPopup.Builder(requireContext()).asConfirm(
                    resources.getString(com.xnfh.framework.R.string.str_tips),
                    resources.getString(R.string.pop_logout_tips)
                ) {
                    LoginServiceProvider.logout(ActManager.Instance().currActivityContext)//跳转到用户登录页面
                    TokenManager.clearUserToken()//清除请求头的token
                    UserInfoManager.instance.loginOut()//清除用户信息
                }.show()
            }
            mBind.mineUserMessage ->{
                UserInforActivity.start(requireContext())
            }
            mBind.mineFeedback ->{
                FeedbackActivity.start(requireContext())
            }
            mBind.mineAboutApp ->{

            }
        }
    }
}